# Create variables for survey analysis



# Setup -----
library(tidyverse)
library(lubridate) 



################################################################################
# Load survey data as "SurveyAll"
# SOURCE: please directly contact the authors of You and Kousky (2024)





# data filtering --------------------------------------------------------------


a = SurveyAll %>%                 # keep those sustained building or contents damage
  filter(damage_home=="Yes" |
         damage_contents=="Yes") 






#####################################################################
##################### Variable Construction #########################
#####################################################################

a$EndDate = as.Date(a$EndDate, format = "%m/%d/%Y")

a$ResponseMonth = ifelse(a$Event.Name=="Hurricane Harvey",   interval(as.Date("2017-08-25"), a$EndDate) %/% months(1), 
                         ifelse(a$Event.Name=="Hurricane Michael",  interval(as.Date("2018-10-10"), a$EndDate) %/% months(1), 
                                ifelse(a$Event.Name=="Hurricane Florence", interval(as.Date("2018-09-14"), a$EndDate) %/% months(1),
                                       ifelse(a$Event.Name=="Hurricane Ida",      interval(as.Date("2021-08-29"), a$EndDate) %/% months(1), NA
                                       ))))


############ Demographic #################
# Gender ---------------------------
a$Male = ifelse(a$gender=="Male", 1, 0)

a$gender = factor(a$gender) %>%
  relevel(ref = "I prefer not to answer") %>%
  relevel(ref = "Male") %>%
  relevel(ref = "Female")

# Education ------------------------- 
a$bach = ifelse(a$educ=="Bachelor's degree" | 
                  a$educ=="Master's Degree" |
                  a$educ=="Professional degree beyond a bachelor's degree" , 1, 0)

# Employment ------------------------- 
a$employment[ a$employment_7_TEXT=="Disability" ] = "Disabled"
a$employment[ a$employment_7_TEXT=="disabled" ] = "Disabled"
a$employment[ a$employment_7_TEXT=="Disabled" ] = "Disabled"
a$employment[ a$employment_7_TEXT=="Disabled " ] = "Disabled"
a$employment[ a$employment_7_TEXT=="Employed full time and also caregiver for 3 Alzheimer's old ladies" ] = "Employed full-time"
a$employment[ a$employment_7_TEXT=="I have always been self employed with irregular income, some investments and I learned to try to save money for emergencies but I never quite expected a CAT 5 hurricane and the consequences.." ] = "Self employed"
a$employment[ a$employment_7_TEXT=="Part time self employment" ] = "Self employed"
a$employment[ a$employment_7_TEXT=="Self e.ployed" ] = "Self employed"
a$employment[ a$employment_7_TEXT=="Self employed " ] = "Self employed"
a$employment[ a$employment_7_TEXT=="Retired and working part time" ] = "Employed part-time"
a$employment[ a$employment_7_TEXT=="entertainment " ] = "Entertainment"

a$employment_new = ifelse(a$employment=="Employed full-time", "Employed full-time",
                          ifelse(a$employment=="Employed part-time" | 
                                   a$employment=="Self employed" |
                                   a$employment=="Entertainment", "Employed part-time/Self employed", 
                                 ifelse(a$employment=="Retired", "Retired", 
                                        ifelse(a$employment=="I prefer not to answer", "I prefer not to answer", "Other"))))

a$employment_new = factor(a$employment_new) %>%
  relevel(ref = "I prefer not to answer") %>%
  relevel(ref = "Other") %>%
  relevel(ref = "Employed part-time/Self employed") %>%
  relevel(ref = "Retired") %>%
  relevel(ref = "Employed full-time")


a$employment_fulltime              = ifelse(a$employment_new=="Employed full-time", 1, 0)
a$employment_retired               = ifelse(a$employment_new=="Retired", 1, 0)
a$employment_parttime_selfemployed = ifelse(a$employment_new=="Employed part-time/Self employed", 1, 0)
a$employment_Other                 = ifelse(a$employment_new=="Other", 1, 0)

# Race ------------------------------
a$white = ifelse(a$race=="White, non-Hispanic" | a$race=="White", 1, 0)
a$nonwhite = 1 - a$white




# Income ----------------------------
a$hhi_less_20000  = ifelse(a$hhi=="Less than $20,000",  1, 0)
a$hhi_20000_34999 = ifelse(a$hhi=="$20,000 to $34,999", 1, 0)
a$hhi_35000_49999 = ifelse(a$hhi=="$35,000 to $49,999", 1, 0)
a$hhi_50000_74999 = ifelse(a$hhi=="$50,000 to $74,999", 1, 0)
a$hhi_75000_99999 = ifelse(a$hhi=="$75,000 to $99,999", 1, 0)
a$hhi_more_100000 = ifelse(a$hhi=="More than $100,000", 1, 0)


a$hhi[ a$hhi=="Other (Please explain)" ] = "I prefer not to answer"

a$hhi = factor(a$hhi) %>%
  relevel(ref="I prefer not to answer") %>%
  relevel(ref="Less than $20,000") %>%
  relevel(ref="$20,000 to $34,999") %>%
  relevel(ref="$35,000 to $49,999") %>%
  relevel(ref="$50,000 to $74,999") %>%
  relevel(ref="$75,000 to $99,999") %>%
  relevel(ref="More than $100,000") 

a$hhi_low    = ifelse(a$hhi_less_20000==1  | a$hhi_20000_34999==1, 1, 0)
a$hhi_median = ifelse(a$hhi_35000_49999==1 | a$hhi_50000_74999==1, 1, 0)
a$hhi_high   = ifelse(a$hhi_75000_99999==1 | a$hhi_more_100000==1, 1, 0)




############ Home-Related #################

# Owner or a Renter of the damaged dwelling? -----------------------------------
a$renter = ifelse(a$home_ownership=="Rent", 1, 0)
a$home_ownership = factor(a$home_ownership) %>%
  relevel(ref = "Own")



# Damaged Dwelling Residence Type ----------------------------------------------
a$single_family = ifelse(a$home_type=="Free standing single-family house", 1, 0)



# At the time of disaster, how long had you been living at your address --------
a$home_tenure = substr(a$home_tenure, 1, nchar(a$home_tenure)-5)
a$home_tenure[ a$home_tenure=="less than 1" ] = 0
a$home_tenure = as.numeric(a$home_tenure)
a$log_home_tenure = log(1+a$home_tenure)


# Number of individuals living in the household at time of disaster ------------
a$home_residents_num[ a$home_residents_num=="1 (only myself)" ] = "1 people (including myself)"
a$home_residents_num[ a$home_residents_num=="more than 12 people (including myself)" ] = "12 people (including myself)"
a$home_residents_num_new = substr(a$home_residents_num, 1, nchar(a$home_residents_num)-25)
a$home_residents_num_new = as.integer(a$home_residents_num_new)



# People living in your home at the time of disaster ---------------------------
a$non_children_disability_elderly_pets = ifelse(a$home_residents=="None of the above", 1, 0)



# Did you have a mortgage on the home you owned at the time of disaster --------
a$home_mortgage_dummy = ifelse(a$home_mortgage=="Yes", 1, 0)




# home ownership, income, race, employment: prefer not to answer ---------------
a$home_household_prefer_not_answer = ifelse(a$home_ownership=="I prefer not to answer" |
                                              a$home_mortgage=="I prefer not to answer" |
                                              a$hhi=="I prefer not to answer" |
                                              a$race=="I prefer not to answer" |
                                              a$employment=="I prefer not to answer" , 1, 0)



############ Financial Position #################

# On a scale of 1 to 10, to what extent did you feel that you had enough money 
# in the 3 weeks following disaster to pay all immediate disaster expenses? ---------------------------
a$enough_money_ind = a$enough_money
a$enough_money_ind[a$enough_money=="1 - I did not have enough money"] = 1
a$enough_money_ind[a$enough_money=="10 - I had plenty of money"] = 10
a$enough_money_ind[a$enough_money=="I prefer not to answer" | a$enough_money==""] = NA
a$enough_money_ind = as.integer(a$enough_money_ind)
a$enough_money_low = ifelse(a$enough_money_ind < median(a$enough_money_ind, na.rm = T), 1, 0)




# Compared to just before disaster, how would you characterize your personal financial situation one year after
a$financial_yr_post = factor(a$financial_yr_post) %>%
  relevel(ref = "I prefer not to answer") %>%
  relevel(ref = "Much better") %>%
  relevel(ref = "Better") %>%
  relevel(ref = "Slightly better") %>%
  relevel(ref = "About the same") %>%
  relevel(ref = "Slightly worse") %>%
  relevel(ref = "Worse") %>%
  relevel(ref = "Much worse")

a$financial_yr_post_ind = ifelse(a$financial_yr_post=="I prefer not to answer", NA,
                          ifelse(a$financial_yr_post=="Much worse",      1,
                          ifelse(a$financial_yr_post=="Worse",           2,
                          ifelse(a$financial_yr_post=="Slightly worse",  3,       
                          ifelse(a$financial_yr_post=="About the same",  4,
                          ifelse(a$financial_yr_post=="Slightly better",  5,
                          ifelse(a$financial_yr_post=="Better",           6,
                          ifelse(a$financial_yr_post=="Much better",      7, NA
                          ))))))))


a$financial_yr_post_veryworse_ind = ifelse(a$financial_yr_post=="Much worse" |
                                             a$financial_yr_post=="Worse", 1, 
                                           ifelse(a$financial_yr_post=="I prefer not to answer", NA, 0))




############ Cost Related #################
# 1) home damage; 2) car damage; 3) evacuation costs; 4) service disruption; 5) additional expenses; 6) lost income

# severity of damage to home or contents ------------
a$damage_home_Yes = ifelse(a$damage_home=="Yes", 1, 0)
a$damage_contents_Yes = ifelse(a$damage_contents=="Yes", 1, 0)

a$damage_severity_home_num[a$damage_home=="No"] = 0
a$damage_severity_home_num[a$damage_severity_home=="1Minimal damage" ] = 1 
a$damage_severity_home_num[a$damage_severity_home=="2Minor damage"   ] = 2
a$damage_severity_home_num[a$damage_severity_home=="3Moderate damage"] = 3
a$damage_severity_home_num[a$damage_severity_home=="4Severe damage"  ] = 4
a$damage_severity_home_num[a$damage_severity_home=="5Completely destroyed"] = 5

a$damage_severity_contents_num[a$damage_contents=="No"] = 0
a$damage_severity_contents_num[a$damage_severity_poss=="1Minimal damage" ] = 1 
a$damage_severity_contents_num[a$damage_severity_poss=="2Minor damage"   ] = 2
a$damage_severity_contents_num[a$damage_severity_poss=="3Moderate damage"] = 3
a$damage_severity_contents_num[a$damage_severity_poss=="4Severe damage"  ] = 4
a$damage_severity_contents_num[a$damage_severity_poss=="5Completely destroyed"] = 5



# damage to car -------------------------------------
a$damage_car_Yes = ifelse(a$damage_car=="Yes", 1, 0)
a$damage_car = factor(a$damage_car) %>% relevel(ref = "No")




# Evacuation cost ------------------------------------------------
a$evac_decision[ a$evac_decision=="I prefer not to answer" & a$evac_totaldollars_4>0 ] <- "Yes"
a$evac_decision_Yes = ifelse(a$evac_decision=="Yes", 1, 0)
a$evac_decision = factor(a$evac_decision) %>% relevel(ref = "No")

a$evac_totaldollars_4   = as.numeric(a$evac_totaldollars_4)
a$evac_totaldollars_all = ifelse(a$evac_decision=="No", 0, a$evac_totaldollars_4)
a$log_evac_totaldollars = ifelse(a$evac_decision=="No", 0, log(1+a$evac_totaldollars_4))
a$evac_totaldollars_000 = a$evac_totaldollars_all/1000

a = a %>%
  group_by(Event.Name) %>%
  mutate(evac_totaldollars_high_event = ifelse(is.na(evac_totaldollars_4), 0, 
                                               ifelse(evac_totaldollars_4 > median(evac_totaldollars_4, na.rm=T), 1, 0)),
         evac_totaldollars_veryhigh_event = ifelse(is.na(evac_totaldollars_4), 0, 
                                                   ifelse(evac_totaldollars_4 > quantile(evac_totaldollars_4, c(1/3, 2/3, 1), na.rm=T)[2], 1, 0)))

a$evac_costs_Shelter            = ifelse(grepl("Shelter", a$evac_costs)==T, 1, 0)
a$evac_costs_Food               = ifelse(grepl("Food", a$evac_costs)==T, 1, 0)
a$evac_costs_PersonalHygiene    = ifelse(grepl("Personal hygiene", a$evac_costs)==T, 1, 0)
a$evac_costs_Healthcare         = ifelse(grepl("Healthcare", a$evac_costs)==T, 1, 0)
a$evac_costs_Transportation     = ifelse(grepl("Transportation", a$evac_costs)==T, 1, 0)
a$evac_costs_Familycare_petcare = ifelse(grepl("Family or pet care", a$evac_costs)==T, 1, 0)

a$evac_costs_Transportation[a$evac_costs_7_TEXT=="Trip to my son's home in Charlotte NC"  ] = 1

# service disruption --------------------------------
a$service_disrupt_No  = ifelse(a$service_disrupt=="I did not experience any service disruptions", 1, 0)
a$service_disrupt_Yes = ifelse(a$service_disrupt!="I did not experience any service disruptions" &
                                 a$service_disrupt!="I prefer not to answer", 1, 0)

a$service_disrupt_Electricity    = ifelse(grepl("Electricity", a$service_disrupt)==T, 1, 0)
a$service_disrupt_Water          = ifelse(grepl("Water", a$service_disrupt)==T, 1, 0)
a$service_disrupt_Internet       = ifelse(grepl("Internet/Cable", a$service_disrupt)==T, 1, 0)
a$service_disrupt_Gas            = ifelse(grepl("Gas", a$service_disrupt)==T, 1, 0)
a$service_disrupt_Phone          = ifelse(grepl("Mobile phone services", a$service_disrupt)==T, 1, 0)
a$service_disrupt_Transportation = ifelse(grepl("Transportation", a$service_disrupt)==T, 1, 0)
a$service_disrupt_access_grocery = ifelse(grepl("Access to food/groceries or other retailers", a$service_disrupt)==T, 1, 0)
a$service_disrupt_Banking        = ifelse(grepl("Banking", a$service_disrupt)==T, 1, 0)

a$service_disrupt_Gas[ a$service_disrupt_8_TEXT=="No gas" ] = 1
a$service_disrupt_Gas[ a$service_disrupt_8_TEXT=="Natural gas, sewer,gasoline for auto and generators" ] = 1
a$service_disrupt_Gas[ a$service_disrupt_8_TEXT=="natural gas" ] = 1
a$service_disrupt_Gas[ a$service_disrupt_8_TEXT=="Nat Gas" ] = 1
a$service_disrupt_Gas[ a$service_disrupt_8_TEXT=="Gas, toiletries, access to property" ] = 1
a$service_disrupt_Gas[ a$service_disrupt_8_TEXT=="Gas stations " ] = 1
a$service_disrupt_Gas[ a$service_disrupt_8_TEXT=="Gas" ] = 1
a$service_disrupt_Gas[ a$service_disrupt_8_TEXT=="gas" ] = 1
a$service_disrupt_Gas[ a$service_disrupt_8_TEXT=="availability of gasoline" ] = 1
a$service_disrupt_Gas[ a$service_disrupt_8_TEXT=="Fuel availability " ] = 1

a$service_disrupt_Phone[ a$service_disrupt_8_TEXT=="landline phone service" ] = 1
a$service_disrupt_Phone[ a$service_disrupt_8_TEXT=="Groceries were no problem but almost all stores were closed. I did not have mobile home services but I did hear that the only phone service was cellular AT&T" ] = 1

a$service_disrupt_Electricity_water = ifelse(a$service_disrupt_Electricity==1 | a$service_disrupt_Water==1, 1, 0)
a$service_disrupt_access            = ifelse(a$service_disrupt_access_grocery==1 | a$service_disrupt_Banking==1, 1, 0)
a$service_disrupt_Internet_Phone    = ifelse(a$service_disrupt_Internet==1 | a$service_disrupt_Phone==1, 1, 0)

a$service_disrupt_cost_high = ifelse(a$service_disrupt_cost=="4Major Costs" | a$service_disrupt_cost=="5Extreme Costs", 1, 0)

a$service_disrupt_cost_ind = ifelse(a$service_disrupt_Yes==0, "No Disruption/No Costs", 
                                    ifelse(a$service_disrupt_cost=="I prefer not to answer", "Cost Unknown",
                                           ifelse(a$service_disrupt_cost=="1No Costs", "No Disruption/No Costs",       
                                                  ifelse(a$service_disrupt_cost=="2Minor Costs" | a$service_disrupt_cost=="3Moderate Costs", 
                                                         "Minor/Moderate Cost", "Major/Extreme Cost"))))

a$service_disrupt_cost_ind = factor(a$service_disrupt_cost_ind) %>%
  relevel(ref = "Cost Unknown") %>%
  relevel(ref = "Major/Extreme Cost") %>%
  relevel(ref = "Minor/Moderate Cost") %>%
  relevel(ref = "No Disruption/No Costs")


a$service_disrupt_cost_num[a$service_disrupt=="I did not experience any service disruptions"] = 0
a$service_disrupt_cost_num[a$service_disrupt_cost=="1No Costs"      ] = 1 
a$service_disrupt_cost_num[a$service_disrupt_cost=="2Minor Costs"   ] = 2
a$service_disrupt_cost_num[a$service_disrupt_cost=="3Moderate Costs"] = 3
a$service_disrupt_cost_num[a$service_disrupt_cost=="4Major Costs"   ] = 4
a$service_disrupt_cost_num[a$service_disrupt_cost=="5Extreme Costs" ] = 5


# additional expenses --------------------------
a$costs_additional_Yes = ifelse(a$costs_additional!="None of the above" &
                                  a$costs_additional!="I prefer not to answer", 1, 0)
a$costs_additional_No = ifelse(a$costs_additional=="None of the above", 1, 0)
a$costs_additional_prefer_not_answer = ifelse(a$costs_additional=="I prefer not to answer", 1, 0)

a$petcare_expense        = ifelse(grepl("Pet care expenses", a$costs_additional)==T, 1, 0)
a$medical_expense        = ifelse(grepl("Medical expenses", a$costs_additional)==T, 1, 0)
a$fuel_expense           = ifelse(grepl("Fuel expenses", a$costs_additional)==T, 1, 0)
a$Miscellaneous_supplies = ifelse(grepl("Miscellaneous supplies", a$costs_additional)==T, 1, 0)
a$Debris_expense         = ifelse(grepl("Debris cleanup expenses", a$costs_additional)==T, 1, 0)
a$Landscaping_expense    = ifelse(grepl("Landscaping expenses", a$costs_additional)==T, 1, 0)
a$Legal_fees             = ifelse(grepl("Legal fees", a$costs_additional)==T, 1, 0)
a$transportation_cost    = ifelse(grepl("Increased transportation or commuting costs", a$costs_additional)==T, 1, 0)
a$Temporaryhousing_cost  = ifelse(grepl("Temporary housing costs", a$costs_additional)==T, 1, 0)
a$Higherinsurance_prices = ifelse(grepl("Higher insurance prices", a$costs_additional)==T, 1, 0)

a$petcare_medical_expense = ifelse(a$petcare_expense==1 | a$medical_expense==1, 1, 0)
a$fuel_supplies_expense = ifelse(a$fuel_expense==1 | a$Miscellaneous_supplies==1, 1, 0)
a$Debris_landscaping_expense = ifelse(a$Debris_expense==1 | a$Landscaping_expense==1, 1, 0)
a$transportation_Temporaryhousing_cost = ifelse(a$transportation_cost==1 | a$Temporaryhousing_cost==1, 1, 0)

a$costs_additional_ind  = ifelse(a$costs_additional!="None of the above" &
                                   a$costs_additional!="I prefer not to answer",
                                 "Had additional costs", a$costs_additional)


# lost income -----------------------------------
a$lostincome_dummy = ifelse(a$lost_income=="Yes", 1, 0)




# costs: prefer not to answer -------------------
a$damage_cost_prefer_not_answer = ifelse(is.na(a$damage_severity_home_num) |
                                           is.na(a$damage_severity_contents_num) |
                                           is.na(a$service_disrupt_cost_num) |
                                           a$lost_income=="I prefer not to answer" |
                                           a$costs_additional_prefer_not_answer==1,
                                         1, 0)


a$damage_severity_home_num = ifelse(is.na(a$damage_severity_home_num), 0, a$damage_severity_home_num)
a$damage_severity_contents_num = ifelse(is.na(a$damage_severity_contents_num), 0, a$damage_severity_contents_num)
a$service_disrupt_cost_num = ifelse(is.na(a$service_disrupt_cost_num), 0, a$service_disrupt_cost_num)








# Funding source -----------------------------------
a$fundingsource_Homeowners_renters_insurance = ifelse(grepl("Homeowners or renters insurance", a$funds_source)==T, 1, 0)
a$fundingsource_Flood_insurance              = ifelse(grepl("Flood insurance", a$funds_source)==T, 1, 0)
a$fundingsource_FEMA_grant                   = ifelse(grepl("A grant from FEMA", a$funds_source)==T, 1, 0)
a$fundingsource_SBA_loan                     = ifelse(grepl("A loan from the SBA", a$funds_source)==T, 1, 0)
a$fundingsource_bank_loan                    = ifelse(grepl("A formal loan from a private bank or other lender", a$funds_source)==T, 1, 0)
a$fundingsource_Family_friends               = ifelse(grepl("Friends or family", a$funds_source)==T, 1, 0)
a$fundingsource_charity_nonprofit            = ifelse(grepl("A charity, non-profit, or community group", a$funds_source)==T, 1, 0)
a$fundingsource_Myemployer                   = ifelse(grepl("My employer", a$funds_source)==T, 1, 0)
a$fundingsource_Local_goverment              = ifelse(grepl("My local government", a$funds_source)==T, 1, 0)
a$fundingsource_Mysavings                    = ifelse(grepl("My own savings", a$funds_source)==T, 1, 0)
a$fundingsource_credit_card                  = ifelse(grepl("A credit card", a$funds_source)==T, 1, 0)
a$fundingsource_AnyIns                       = ifelse(a$Homeowners_renters_insurance_fundingsource==1 | a$Flood_insurance_fundingsource==1, 1, 0)


a$fundingsource_number_new = a$fundingsource_AnyIns+
  a$fundingsource_FEMA_grant +
  a$fundingsource_SBA_loan +
  a$fundingsource_bank_loan +
  a$fundingsource_Family_friends +
  a$fundingsource_charity_nonprofit +
  a$fundingsource_Myemployer +
  a$fundingsource_Local_goverment +
  a$fundingsource_Mysavings +
  a$fundingsource_credit_card

a$fundingsource_number_new[a$funds_source_12_TEXT=="Landlord"] = 1







# Application for Federal Assistance -------------------------------------------
a$apply_FEMA_Yes    = ifelse(a$fundingsource_FEMA_grant==1, 1, ifelse(a$apply_FEMA=="I prefer not to answer", NA, ifelse(grepl("Yes, I applied", a$apply_FEMA)==T, 1, 0)))
a$approved_FEMA_Yes = ifelse(a$fundingsource_FEMA_grant==1, 1, ifelse(a$apply_FEMA=="I prefer not to answer", NA, ifelse(grepl("Yes, I applied and I was approved", a$apply_FEMA)==T, 1, 0)))
a$apply_SBA_Yes     = ifelse(a$fundingsource_SBA_loan==1, 1, ifelse(a$apply_SBA=="I prefer not to answer", NA, ifelse(grepl("Yes, I applied", a$apply_SBA)==T, 1, 0)))
a$approved_SBA_Yes  = ifelse(a$fundingsource_SBA_loan==1, 1, ifelse(a$apply_SBA=="I prefer not to answer", NA, ifelse(grepl("Yes, I applied and I was approved", a$apply_SBA)==T, 1, 0)))










# Did you use your own savings to cover any financial gaps -----------
a$savings[ a$savings=="Yes, I used my savings, but when combined with other sources of funds, it was not sufficient to cover all of my costs.  " ] = "Yes, I used my savings, but when combined with other sources of funds, it was not sufficient to cover all of my costs."
a$savings[ a$savings=="I prefer not to answer" ] = "I prefer not to answer."
a$savings[ a$savings=="No. I did not have savings at that time." ] = "No, I did not have savings at that time."
a$savings[ a$savings=="No. I had savings at that time, but I did not use them." ] = "No, I had savings at that time, but I did not use them."

a$savings_ind = ifelse(a$savings=="I prefer not to answer." | 
                         a$savings=="No, I did not have savings at that time.", a$savings,
                       "Yes, I had savings.")

a$savings_ind = factor(a$savings_ind) %>%
  relevel(ref = "No, I did not have savings at that time.")

a$savings_yes = ifelse(a$savings_ind=="Yes, I had savings.", 1, 0)

# Insurance-related --------------------------------
a$AnyIns              = ifelse(a$ins_flood_event=="Yes" | a$ins_home_rent=="Yes", 1, 0)
a$ins_flood_event_Yes = ifelse(a$ins_flood_event=="Yes", 1, 0)
a$ins_home_rent_Yes   = ifelse(a$ins_home_rent=="Yes", 1, 0)

a$ins_flood_switched = ifelse(a$ins_flood_event=="No" & a$ins_flood_current=="Yes", 1,
                       ifelse(a$ins_flood_event=="I prefer not to answer" | a$ins_flood_current=="I prefer not to answer", NA, 0))


a$help_ins_new = a$help_ins
a$help_ins_new[ a$help_ins=="1 - Not at All Useful" ] <- 1
a$help_ins_new[ a$help_ins=="10 - Extremely Useful" ] <- 10
a$help_ins_new[ a$help_ins=="I prefer not to answer" ] <- NA
a$help_ins_new = as.integer(a$help_ins_new)








## external sources fully cover all your financial costs --------------------
a$total_coverage[a$total_coverage=="I prefer not to answer" & 
                   a$savings=="Yes, I used my savings, and when combined with other sources of funds, it was sufficient to cover all my costs."] <- "No"

a$total_coverage[a$total_coverage=="I prefer not to answer" & 
                   a$savings=="Yes, I used my savings, but when combined with other sources of funds, it was not sufficient to cover all of my costs."] <- "No"

a$external_fullycover_cost = ifelse(a$total_coverage=="I prefer not to answer", NA, ifelse(a$total_coverage=="Yes", 1, 0))

## Unmet needs --------------------------------------------------------------

a$Met_Needs = ifelse(a$total_coverage=="Yes", 1, 
              ifelse(a$total_coverage=="No" & a$savings=="Yes, I used my savings, and when combined with other sources of funds, it was sufficient to cover all my costs.", 1, 0))

a$Unmet_Needs = ifelse(a$total_coverage=="I prefer not to answer" & a$savings=="I prefer not to answer.", NA, 1-a$Met_Needs)








## Event Name -------------------------------------------------------------
a$Event.Name = factor(a$Event.Name) %>%
  relevel(ref = "Hurricane Ida") %>%
  relevel(ref = "Hurricane Florence") %>%
  relevel(ref = "Hurricane Michael") %>%
  relevel(ref = "Hurricane Harvey")

a$prefer_not_answer = ifelse(a$damage_cost_prefer_not_answer==1 | a$home_household_prefer_not_answer==1, 1, 0)



save(a, file="/02_Data/a.RData")